<?php

namespace app\admin\controller;

use app\admin\logic\Form5Logic;
use app\admin\model\Community;
use app\admin\model\Form7;
use app\admin\model\TemperatureAlarm;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="集中隔离人员信息")
 */
class Form5 extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Form5();
        
        $this->assign('getSexList', $this->model->getSexList());

        $this->assign('getIsDangerLocationList', $this->model->getIsDangerLocationList());

        $this->assign('getCommunityList', Community::getCommunityList());

    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();

            if (!$this->isSuper) {
                $where[] = ['community', '=', $this->user->community_id];
            }

            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'name|姓名'                           =>  'require|chs',
                'sex|性别'                            =>  'require',
                'id_card|身份证号'                     =>  'require|idCard',
                'buy_way|行程出发地、路径地及时间'      =>  'require',
                'mobile|手机号'                        =>  'require|mobile',
                'departure|出发地'                     =>  'require',
                'living|现居住地址'                     =>  'require',
                'employer|工作单位'                     =>  'require',
                'arrival_time|到沙时间'                 =>  'require|date',
                'observe|留观点'                        =>  'require',
                'is_danger_location|高中风险地区'       =>  'require',
                'person_type|人员类别'                  =>  'require',
                'jc_time|预计接触隔离时间'               =>  'require|date',
                'desc|备注'                             =>  'require',
            ];
            $this->validate($post, $rule);

            if (!$this->isSuper) {
                $post['community'] = $this->user->community_id;
            } else {
                empty($post['community']) && $this->error('去向不能为空');
            }
            if (strtotime($post['arrival_time']) > time()) {
                $this->error('到沙时间不能大于当前');
            }

            //当有未解除的，还重复提交时，将旧的标注为已废弃
            $old = $this->model->where([
                'id_card' => $post['id_card'],
                'status'  => 0,
            ])->order('id', 'desc')->limit(1)->find();
            if (!empty($old)) {
                //是否继承发信时间和天数
//                $post['count'] = $old->count;
//                $post['sms_time'] = $old->sms_time;
                $old->status = 2;
                $old->save();
            }

            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $row = $this->model->find($this->request->param($this->model->getPk()));
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'name|姓名'                           =>  'require|chs',
                'sex|性别'                            =>  'require',
                'id_card|身份证号'                     =>  'require|idCard',
                'buy_way|行程出发地、路径地及时间'      =>  'require',
                'mobile|手机号'                        =>  'require|mobile',
                'departure|出发地'                     =>  'require',
                'living|现居住地址'                     =>  'require',
                'employer|工作单位'                     =>  'require',
                'arrival_time|到沙时间'                 =>  'require|date',
                'observe|留观点'                        =>  'require',
                'is_danger_location|高中风险地区'       =>  'require',
                'person_type|人员类别'                  =>  'require',
                'jc_time|预计接触隔离时间'               =>  'require|date',
                'desc|备注'                             =>  'require',
            ];
            $this->validate($post, $rule);

            if (!$this->isSuper) {
                $post['community'] = $this->user->community_id;
            } else {
                empty($post['community']) && $this->error('去向不能为空');
            }

            if (strtotime($post['arrival_time']) > time()) {
                $this->error('到沙时间不能大于当前');
            }

            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="体温记录")
     */
    public function temperature_index()
    {
        return $this->temperature_index_common('form5');
    }

    /**
     * @NodeAnotation(title="添加体温记录")
     */
    public function temperature_add()
    {
        return $this->temperature_add_common();
    }

    /**
     * @NodeAnotation(title="导入")
     * @return mixed
     */
    public function import()
    {
        if ($this->request->isAjax()) {
            $post = [
                'upload_type' => 'local',
                'file'        => $this->request->file('file'),
            ];

            $uploadConfig = sysconfig('upload');
            $rule = [
                'upload_type|指定上传类型有误' => "in:{$uploadConfig['upload_allow_type']}",
                'file|文件'                    => "require|file|fileExt:{$uploadConfig['upload_allow_ext']}|fileSize:{$uploadConfig['upload_allow_size']}",
            ];
            $this->validate($post, $rule);

            $result = Form5Logic::import($post);
            if ($result === false){
                $this->error(self::getError());
            }
            $this->success('导入成功');
        }
        $this->assign('download_url', url('ajax/download_templet', ['name' => '集中隔离人员信息导入表.xlsx']));
        return $this->fetch('common/import');
    }
}